#!/usr/bin/python3 -O

import os
import sys
import random
import argparse
import signal

def handler(signum, frame):
    raise OSError("User Abort!")

#set the signal handler
signal.signal(signal.SIGTERM, handler)

myargprs = argparse.ArgumentParser (
    prog = 'svn', 
    usage = './svn <svn cmd> [svn args] [++project <project name>] [++debug <debug level>]',
    description = 'local svn command that acts over the whole project',
    epilog = 'epilogo de svn',
    version = '1.0',
    parents = [],
    formatter_class = argparse.HelpFormatter,
    prefix_chars = '+',
    fromfile_prefix_chars = None,
    argument_default = None,
    conflict_handler='error',
    add_help = True)

myargprs.add_argument(
    '++project',
    nargs = 1,
    const = None,
    default = 'vw/trunk',
    help = 'which project folder should be used',
    metavar = '<project name>')

myargprs.add_argument(
    '++debug',
    nargs = 1,
    const = None,
    default = ['0'],
    help = 'which level of info to get out',
    metavar = '<debug level>')

myargprs.add_argument(
    dest = 'command',
    nargs = 1,
    const = None,
    default = 'ls',
    help = 'svn command to run',
    metavar = '<svn cmd>')

myargprs.add_argument(
    'svnopt',
    nargs = '*',
    const = None,
    default = None,
    help = 'svn arguments to run',
    metavar = '[svn args]')

args = myargprs.parse_args(sys.argv[1:])
proyecto = args.project[0]
debug = int(args.debug[0])
command = args.command[0]
svnopt = args.svnopt
#if debug== 1 : 
print ('proyecto ->', proyecto, '-')
print ('debug ->', debug, '-')
print ('command ->', command, '-')
print ('svnopt ->', svnopt, '-')

hdcmd = 0

wc = os.getcwd() + os.path.sep + proyecto + os.path.sep

svnarg = ''
for myarg in svnopt :
    svnarg = svnarg + ' ' + myarg

if command == "export" :
    hdcmd = 1
    svncmd = 'svn ' + command + ' --config-dir /home/amesagarcia export '
elif command == "co" or command == "checkout" :    
    hdcmd = 2
    svncmd = 'svn ' + command + ' --config-dir /home/amesagarcia '
else :
    hdcmd = 3
    svncmd = 'svn ' + command + ' --config-dir /home/amesagarcia ' + svnarg

rndfile = "/tmp/svn" + str(random.randint(1000,9999))
cmdlist = "/lib/xerces-c/DOMPrint.exe -xpath=/psf/provider/project -wfpp=on " + wc + "configuration" + os.path.sep + "projectSet.psf | awk -v FS=, '{if(length()>0) print $2 FS $3}' 1>" + rndfile
if debug == 1 :
    print ('hdcmd ->', hdcmd, '-')
    print ('svncmd ->', svncmd, '-')
    print ('rndfile ->', rndfile, '-')
    print ('cmdlist ->', cmdlist, '-')

os.system(cmdlist)
myfile = open(rndfile,"r")

mylist = os.listdir(wc)

for myelemhelp in myfile:
    myelem0 = myelemhelp.splitlines()[0]
    [myrep, myelem] = myelem0.split(',')
    try :
        if os.path.isfile(wc + myelem + os.path.sep + 'Project.meta') or hdcmd == 2 :
            if hdcmd == 1 :
                if debug == 1 : print (svncmd + ' ' + wc + myelem + ' ' + sys.argv[2] + myelem)
                if debug == 2 : print ('----')
                os.system(svncmd + ' ' + wc + myelem + ' ' + sys.argv[2] + myelem)
            elif hdcmd == 2 :
                if debug == 1 : print (svncmd + ' ' + myrep + ' ' + wc + myelem)
                if debug == 2 : print ('----')
                os.system(svncmd + ' ' + myrep + ' ' + wc + myelem)
            elif hdcmd == 3 :
                if debug == 1 : print (svncmd + ' ' + wc + myelem)
                if debug == 2 : print ('----')
                os.system(svncmd + ' ' + wc + myelem)
        else :
            print ("error -" + myelem + "-")
        
    except :
        print (myelem)
        print (sys.exc_info())
        continue

myfile.close()
os.remove(rndfile)
